﻿using AnesSystem.Controls;
using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Udp;
using AnesSystem.Util;
using AnesSystem.ViewModels.LaborAnalgesia;
using Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace AnesSystem.ViewModels
{
    public class FunctionListHeaderCtrlViewModel : BaseViewModel<FunctionListHeaderCtrlViewModel>
    {
        #region Variables

        private ObservableCollection<CommonBtnCtrlModel> _listCtrl;
        private Visibility _visibility = Visibility.Collapsed;


        #endregion

        #region Constructor

        public FunctionListHeaderCtrlViewModel()
        {
            _listCtrl = new ObservableCollection<CommonBtnCtrlModel>();
            InitialListCtrl();

        }

        #endregion

        #region Properties

        public ObservableCollection<CommonBtnCtrlModel> ListCtrl
        {
            get
            {
                return _listCtrl;
            }

            set
            {
                _listCtrl = value;
                RaisePropertyChanged("ListCtrl");
            }
        }

        public Visibility Visibility
        {
            get
            {
                return _visibility;
            }
            set
            {
                _visibility = value;
                RaisePropertyChanged("Visibility");
            }
        }

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        private void InitialListCtrl()
        {
            foreach (FunctionListButton item in Enum.GetValues(typeof(FunctionListButton)))
            {
                CommonBtnCtrlModel model = new CommonBtnCtrlModel(item.ToString(), false, "", @"..\..\images\header\" + item.ToString() + ".png");
                ListCtrl.Add(model);
            }
        }

        #endregion

        #region Public/Protected Methods

        public void Excute(CommonBtnCtrlModel commond)
        {
            FunctionListButton com = (FunctionListButton)Enum.Parse(typeof(FunctionListButton), commond.Name);
            switch (com)
            {
                case FunctionListButton.打印:
                    Print();
                    break;
                case FunctionListButton.取消手术:
                    CancelOperCtrl();
                    break;
                case FunctionListButton.菜单设置:
                    MenuSet();
                    break;
                case FunctionListButton.手术详情:
                    OperationDetailsCtrl();
                    break;
                case FunctionListButton.模板:
                    AnesTemplateCtrl();
                    break;
                case FunctionListButton.储存模板:
                    AddAnesTemplateCtrl();
                    break;
                case FunctionListButton.全屏:
                    FullScreen();
                    break;
                case FunctionListButton.入复苏室:
                    InPacu();
                    break;
                case FunctionListButton.出复苏室:
                    OutPacu();
                    break;
                case FunctionListButton.检验数据:
                    GetLisData();
                    break;
                case FunctionListButton.刷新:
                    Ref();
                    break;
                case FunctionListButton.导出:
                    Excel();
                    break;
                case FunctionListButton.提交:
                    Submit();
                    break;
                case FunctionListButton.保存:
                    Save();
                    break;
                case FunctionListButton.术中输血:
                    BloodInOper();
                    break;
                case FunctionListButton.开始时间:
                    LaborStart();
                    break;
                case FunctionListButton.镇痛模板:
                    LaborTemplate();
                    break;
                case FunctionListButton.存储模板:
                    AddLaborTemplate();
                    break;
                case FunctionListButton.实时监控:
                    RealTimeJianKong();
                    break;
            }
        }

        private void RealTimeJianKong()
        {
            MessageBoxCommon.ShowDialog("当前病人麻醉单及文书已被主任质控管理进行修改，痕迹已保留，是否更新？", MessageBoxCommon.OKCANCLE);
        }

        private void AddLaborTemplate()
        {
            try
            {
                LaborTemplateAddCtrl templateAddCtrl = new LaborTemplateAddCtrl();
                DrugAndEventWindow.Instance.Title = "新建镇痛模板";
                DrugAndEventWindow.Instance.CusContent = templateAddCtrl;
                DrugAndEventWindow.Instance.ShowDialog();
            }
            catch (Exception ex)
            {

                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        private void LaborTemplate()
        {

            try
            {
                LaborTemplateCtrl templateAddCtrl = new LaborTemplateCtrl();
                DrugAndEventWindow.Instance.Title = "镇痛模板";
                DrugAndEventWindow.Instance.CusContent = templateAddCtrl;
                DrugAndEventWindow.Instance.ShowDialog();
            }
            catch (Exception ex)
            {

                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        private void LaborStart()
        {
            LaborAnalgesiaProcessViewModel.Instance.StartLabor();
        }

        public void BloodInOper()
        {
            if (string.IsNullOrEmpty(DataManager.Instance.CurrentPatientLink.AnesthesiaRecord.Zmys))
            {
                MessageBoxCommon.ShowDialog("主麻医生尚未签名，无法打开此程序！", MessageBoxCommon.OK);
                return;
            }
            AccountModel account = DataManager.Instance.AccountList.Where(O => O.Id.ToString().Equals(DataManager.Instance.CurrentPatientLink.AnesthesiaRecord.Zmys)).First();
            List<ConfigDepartmentsModel> departmentList = DataManager.Instance.ConfigDepartmentsModelList.Where(O => O.Name.Equals(DataManager.Instance.CurrentPatientLink.Patient.Ksmc)).ToList();
            ConfigDepartmentsModel department = null;
            if (departmentList == null || departmentList.Count < 1)
            {
                MessageBoxCommon.ShowDialog("科室名称与his端不匹配，无法打开此程序！", MessageBoxCommon.OK);
                return;
            }
            department = departmentList.First();
            string paramStr = "2-" + account.WorkID + "-" + department.Code + "-" + DataManager.Instance.CurrentPatientLink.Patient.Zyh;
            //string param = "2-1001-1027-201704042";
            CheckAndOpenExe(paramStr);
        }

        #region 通过当前代码执行路径向上找到相关exe，并根据processes.Length判断是否已启动

        private bool CheckAndOpenExe(string param)
        {
            string exeName = "pbcom";
            Process[] processes = Process.GetProcessesByName(exeName);
            if (processes.Length > 0)
            {
                MessageBoxCommon.ShowDialog("程序已启动，请勿重复启动！", MessageBoxCommon.OK);
                return true;
            }
            else
            {
                return OpenExe(param);
            }
        }

        private bool OpenExe(string param)
        {
            Process pr = new Process();
            try
            {                
                Process.Start("D:\\pb\\pbcom\\pbcom.exe", param);
                //pr.StartInfo.FileName = string.Format(@"E:\\Bochuang\\pbcom\\pbcom_test.exe", AssemblyDirectory);
                //pr.Start();
                pr.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                return true;
            }
            catch
            {
                MessageBoxCommon.ShowDialog("未找到此程序！", MessageBoxCommon.OK);
                return false;
            }
            finally
            {
                if (pr != null)
                {
                    pr.Close();
                }
            }
        }

        public static string AssemblyDirectory
        {
            get
            {
                string codeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
                UriBuilder uri = new UriBuilder(codeBase);
                string path = Uri.UnescapeDataString(uri.Path);
                return Path.GetDirectoryName(path);
            }
        }//获取当前代码运行的目录

        #endregion
        private void Save()
        {
            PatientLinkService.Instance.AddUpdate(DataManager.Instance.CurrentPatientLink);
            MessageBoxCommon.ShowDialog("保存成功", MessageBoxCommon.OK);
        }

        private void InPacu()
        {
            PacuProcessViewModel.Instance.InPacuClick();
        }
        private void OutPacu()
        {
            PacuProcessViewModel.Instance.OutPacuClick();
        }

        private void Submit()
        {
            (DataManager.Instance.CurrentControl as OperationNoticeCtrl).SendSchedueData();
        }

        private void Excel()
        {
            ExportToExcel ex = new ExportToExcel();
            List<PatientLinkModel> datagrids = (DataManager.Instance.CurrentControl as OperationNoticeCtrl).GetPatientLink();
            ex.ExportParameterCommon("手术通知单", datagrids);


        }

        private void Ref()
        {
            if (DataManager.Instance.CurrentControl is BasePrintControl)
            {
                (DataManager.Instance.CurrentControl as BasePrintControl).Refesh();
            }
        }

        private void FullScreen()
        {
            FullWindow.Instance.CusContent = DataManager.Instance.RightContentCtrlViewModel.TableContent;
            FullWindow.Instance.ShowInTaskbar = false;
            FullWindow.Instance.ShowDialog();
        }
        public void GetLisData()
        {
            (DataManager.Instance.CurrentControl as CustomCommonTableCtrl).GetLisData();
        }

        public void Print()
        {
            if (DataManager.Instance.CurrentControl is BaseAnesRecordCtrl)
            {
                if (DataManager.Instance.CurrentPatientLink.Application.Detailstatus.Equals(Constants.OR_STATUS_FLAG_IN_ROOM))
                {
                    MessageBoxCommon.ShowDialog("当前病人未出室，无法打印！", MessageBoxCommon.OK);
                    return;
                }
                if (DataManager.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_CQ_NTZYY))
                {
                    if (!CheckBaseInfo())
                    {
                        MessageBoxCommon.ShowDialog("请将病人信息填写完整！", MessageBoxCommon.OK);
                        return;
                    }
                }
            }
            if (DataManager.Instance.CurrentControl is BasePacuRecordCtrl)
            {
                if (DataManager.Instance.CurrentPatientLink.Application.Detailstatus.Equals(Constants.OR_STATUS_FLAG_IN_PCU))
                {
                    MessageBoxCommon.ShowDialog("当前病人未出室，无法打印！", MessageBoxCommon.OK);
                    return;
                }
            }
            if (PrintTemplate.PrintVisul != null)
            {
                PrintTemplate.PrintVisul.Print();
            }
        }

        //南桐打印前判断
        private bool CheckBaseInfo()
        {

            PatientLinkModel patientLink = DataManager.Instance.CurrentPatientLink;
            bool flag = true;
            if (string.IsNullOrEmpty(patientLink.Patient.Brxm))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.Patient.Xb))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.Patient.Tz))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.Patient.Sg))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.Patient.Ksmc))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.Patient.Zych))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.Patient.Zyh))
            {
                flag = false;
            }
            if (!PatientLink.AnesthesiaRecord.Asa.Jz && !patientLink.AnesthesiaRecord.Asa.Zq)
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.AnesthesiaRecord.Asa.Level))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.AnesthesiaRecord.Nyha))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.AnesthesiaRecord.Nnis))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.Patient.Xx))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.AnesthesiaRecord.Mzqyy))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.AnesthesiaRecord.Sstw))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.AnesthesiaRecord.Mzff))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.Application.Sqzd1))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.Application.Nsssmc))
            {
                flag = false;
            }
            if (!patientLink.AnesthesiaRecord.Js && !patientLink.AnesthesiaRecord.Bjs)
            {
                flag = false;
            }
            if (!patientLink.AnesthesiaRecord.Jhss && !patientLink.AnesthesiaRecord.Fjhss)
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.AnesthesiaRecord.Knqd))
            {
                flag = false;
            }
            if (!patientLink.AnesthesiaRecord.Js && !patientLink.AnesthesiaRecord.Bjs)
            {
                flag = false;
            }
            if (!patientLink.AnesthesiaRecord.Flwxx_g && !patientLink.AnesthesiaRecord.Flwxx_z && !patientLink.AnesthesiaRecord.Flwxx_d)
            {
                flag = false;
            }
            if (!patientLink.AnesthesiaRecord.PONV_gw && !patientLink.AnesthesiaRecord.PONV_zw && !patientLink.AnesthesiaRecord.PONV_dw)
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.AnesthesiaRecord.Tsqk))
            {
                flag = false;
            }
            if (!patientLink.AnesthesiaRecord.PCIA && !patientLink.AnesthesiaRecord.PCEA && !patientLink.AnesthesiaRecord.PCNA && !patientLink.AnesthesiaRecord.WyPCA)
            {
                flag = false;
            }
            if (!patientLink.AnesthesiaRecord.ComplicatedMethod.Zm1 && !patientLink.AnesthesiaRecord.ComplicatedMethod.Zm2 && !patientLink.AnesthesiaRecord.ComplicatedMethod.Zm3)
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.AnesthesiaRecord.Sxpf))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.AnesthesiaRecord.Brqw))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.AnesthesiaRecord.Zmys))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(patientLink.AnesthesiaRecord.OperDoctor1))
            {
                flag = false;
            }

            return flag;

        }

        public void MenuSet()
        {
            MenuMangeCtrl ctrl = new MenuMangeCtrl();
            DrugAndEventWindow.Instance.Title = "";
            DataManager.Instance.RightContentCtrlViewModel.CurrentRightContent = ctrl;
        }

        public void OperationDetailsCtrl()
        {
            try
            {
                OperationDetailsWin operationDetailsCtrl = new OperationDetailsWin();
                operationDetailsCtrl.ShowDialog();
            }

            catch (Exception ex)
            {

                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        public void AnesTemplateCtrl()
        {
            try
            {
                AnesTemplateCtrl anesTemplateCtrl = new AnesTemplateCtrl();
                DrugAndEventWindow.Instance.Title = "模板";
                DrugAndEventWindow.Instance.CusContent = anesTemplateCtrl;
                DrugAndEventWindow.Instance.ShowDialog();
            }
            catch (Exception ex)
            {

                Common.LogHelper.Error(this.GetType(), ex);
            }
        }
        public void AddAnesTemplateCtrl()
        {
            try
            {
                TemplateAddCtrl templateAddCtrl = new TemplateAddCtrl();
                DrugAndEventWindow.Instance.Title = "新建模板";
                DrugAndEventWindow.Instance.CusContent = templateAddCtrl;
                DrugAndEventWindow.Instance.ShowDialog();
            }
            catch (Exception ex)
            {

                Common.LogHelper.Error(this.GetType(), ex);
            }
        }
        public void CancelOperCtrl()
        {
            try
            {
                CancelOperCtrl cancelOperCtrl = new CancelOperCtrl();
                DrugAndEventWindow.Instance.Title = "取消手术";
                DrugAndEventWindow.Instance.CusContent = cancelOperCtrl;
                DrugAndEventWindow.Instance.ShowDialog();
            }
            catch (Exception ex)
            {

                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        public void AllDisplay(bool isDisplay)
        {
            foreach (var item in ListCtrl)
            {
                item.State = isDisplay;
            }
            this.Visibility = Visibility.Visible;
        }

        public void DisplayButton(bool isDisplay, params FunctionListButton[] btns)
        {
            AllDisplay(!isDisplay);
            foreach (var btn in btns)
            {
                foreach (var item in ListCtrl)
                {
                    if (item.Name.Equals(btn.ToString()))
                    {
                        item.State = isDisplay;
                    }
                }
            }
            this.Visibility = isDisplay ? Visibility.Visible : Visibility.Collapsed;
        }






        #endregion

        #region EventHandlers

        #endregion
    }
    public enum FunctionListButton
    {
        打印, 刷新, 模板, 取消手术, 手术详情, 储存模板, 日期, 检验数据, 返回, 模板删除, 菜单设置, 全屏, 入复苏室, 出复苏室, 导出, 提交, 保存, 术中输血, 开始时间, 镇痛模板, 存储模板,实时监控
    }
}
